package com.zhumengquan.wozhi.controller;

import com.alibaba.fastjson.JSONObject;
import com.zhumengquan.wozhi.domain.User;
import com.zhumengquan.wozhi.service.FindUserByNameService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class LoginController {

    @Autowired
    private FindUserByNameService findUserByNameService;

    @RequestMapping("/login")
    @ResponseBody
    public JSONObject FindUserByName (String name, String password, HttpServletRequest requset){
        System.out.println("登录检查执行啦");
        JSONObject jsonObject = new JSONObject();
        User result = findUserByNameService.getUser(name);
        if (result == null){
            jsonObject.put("msg", "error");
            return  jsonObject;
        }

        HttpSession session = requset.getSession();
        session.setAttribute("name", result.getName());


        if (result.getPassword().equals(password)){
            jsonObject.put("msg", "success");
            jsonObject.put("name", name);
            return jsonObject;
        }
        else {
            jsonObject.put("msg", "error");
            return jsonObject;
        }
    }
}
